/**
 * 
 */
package org.extdev.connection.serialport;

import gnu.io.CommPortIdentifier;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

/**
 * @author LAP
 *
 */
public class SerialPortHelper {
	private List<Integer> immutableBaudList;
	private SerialPortHelper() {
		List<Integer> baudList = new ArrayList<Integer>();
		baudList.add(4800);
		baudList.add(9600);
		baudList.add(14400);
		baudList.add(19200);
		baudList.add(38400);
		baudList.add(57600);
		baudList.add(115200);
		baudList.add(128000);
		immutableBaudList = Collections.unmodifiableList(baudList);
	}
	
	private static class SerialPortHelperHolder {
		private static final SerialPortHelper INSTANCE = new SerialPortHelper();
	}
	
	public static SerialPortHelper getInstance() {
		return SerialPortHelperHolder.INSTANCE;
	}
	
    /**
    *
    * @return Enumeration<CommPortIdentifiers>. Enumeration of founded ports.
    * <br> Lots of different interesting information
    */
	private static Enumeration<?> getPorts() {
	       return CommPortIdentifier.getPortIdentifiers();
	   }
		
    /**
     * Returns list with names of existing SerialPorts
     * @return List<String> List of founded COM (serial) ports
     */
    public List<String> getSerialPortsAsStringList() {
        List<String> ports = new ArrayList<String>();
        java.util.Enumeration<?> portEnum = getPorts();
        while (portEnum.hasMoreElements()) {
            Object portIdentifier = portEnum.nextElement();
	        if (portIdentifier instanceof CommPortIdentifier) {
		        if (((CommPortIdentifier)portIdentifier).getPortType() == CommPortIdentifier.PORT_SERIAL) {
		        	ports.add(((CommPortIdentifier)portIdentifier).getName());
		        }
            }
        }
        return ports;
    }
    
    public List<Integer> getSerialportBaudRatesList() {
    	return immutableBaudList;
    }
}
